/**
 * Created by uf040339 on 2016/9/7.
 */

(function ($, global) {
    var getShiftDataTimeHandle,
        loopGetShiftData = function () {
            clearInterval(getShiftDataTimeHandle);
            getShiftData();
            getShiftDataTimeHandle = setInterval(function () {
                getShiftData();
            }, 15000);
        };
    $(function () {
        initLayout(function () {
            $('#tabList a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var currentTarget = e.currentTarget.hash;
                if (currentTarget == '#tab1') {
                    global.loopRefreshCurrentMessages();
                    clearInterval(getShiftDataTimeHandle);
                }
                else {
                    loopGetShiftData();
                    clearInterval(global.refreshCurrentMessagesTimeHandle);
                }
            });
            loopGetShiftData();
            setTimeout(function () {
                $('body').removeClass('loading');
            }, 1000);
        });
    });

    var initLayout = function (callback) {
        var winWidth = $(window).width(),
            winHeight = $(window).height();

        setTimeout(function () {
            //alert('winWidth='+winWidth+';winHeight='+winHeight);
            var $tabList = $('#tabList'),
                $tab1 = $('#tab1'),
                tabListHeight = $tabList.outerHeight(),
                tabBodyHeight = winHeight - tabListHeight,
                graphicWHRatio = 1.5;

            if (global.line_name == 'Z6') {
                graphicWHRatio = 132 / 100;
            }

            // for flexable
            var graphicWt = winWidth,
                graphicHt = graphicWt / graphicWHRatio;

            if (winWidth < winHeight) {
                $tab1.height(tabBodyHeight).css({
                    'overflow-x': 'auto',
                    'overflow-y': 'hidden'
                });
                graphicWt = parseInt(tabBodyHeight * graphicWHRatio);
                graphicHt = tabBodyHeight;
            }
            else {
                $tab1.height(tabBodyHeight).css({
                    'overflow-x': 'hidden',
                    'overflow-y': 'auto'
                });
            }

            $('.graphic-layer').width(graphicWt).height(graphicHt);

            (callback && typeof(callback) === "function") && callback();
        }, 100);
    };

    var fillLineInfo = function (resp, codeVal) {
        if (resp['S' + codeVal]) $('#iptS' + codeVal).html(resp['S' + codeVal]);
        if (resp['E' + codeVal]) $('#iptE' + codeVal).html(resp['E' + codeVal]);

        if (/\d+/.test(resp['TargetOutput' + codeVal])) $('#targetOutput' + codeVal).html(resp['TargetOutput' + codeVal]);
        if (/\d+/.test(resp['TargetInput' + codeVal])) $('#targetInput' + codeVal).html(resp['TargetInput' + codeVal]);
        // 如果 target 值为 0 ，则 current 设置为0（即使有值）
        if (resp['TargetOutput' + codeVal] > 0) {
            if (resp['CurrentOutput' + codeVal]) {
                var $currentOutput = $('#currentOutput' + codeVal);
                $currentOutput.html(resp['CurrentOutput' + codeVal]);
                if (resp['PlannedOutput' + codeVal] > resp['CurrentOutput' + codeVal]) {
                    $currentOutput.addClass('text-danger');
                }
                else {
                    $currentOutput.removeClass('text-danger');
                }
            }
            if (resp['CurrentInput' + codeVal]) {
                var $currentInput = $('#currentInput' + codeVal);
                $currentInput.html(resp['CurrentInput' + codeVal]);
                if (resp['PlannedInput' + codeVal] > resp['CurrentInput' + codeVal]) {
                    $currentInput.addClass('text-danger');
                }
                else {
                    $currentInput.removeClass('text-danger');
                }
            }
            if (resp['ForecastOutput' + codeVal]) {
                var $forecastOutput = $('#forecastOutput' + codeVal);
                $forecastOutput.html(resp['ForecastOutput' + codeVal]);
                if (resp['TargetOutput' + codeVal] > resp['ForecastOutput' + codeVal]) {
                    $forecastOutput.addClass('text-danger');
                }
                else {
                    $forecastOutput.removeClass('text-danger');
                }
            }
            if (resp['ForecastInput' + codeVal]) {
                var $forecastInput = $('#forecastInput' + codeVal);
                $forecastInput.html(resp['ForecastInput' + codeVal]);
                if (resp['TargetInput' + codeVal] > resp['ForecastInput' + codeVal]) {
                    $forecastInput.addClass('text-danger');
                }
                else {
                    $forecastInput.removeClass('text-danger');
                }
            }
            if (resp['PlannedOutput' + codeVal]) {
                var $plannedOutput = $('#plannedOutput' + codeVal);
                $plannedOutput.html(resp['PlannedOutput' + codeVal]);

                if (resp['CurrentOutput' + codeVal]) {
                    var $cpCut = $('#cpCutOutput' + codeVal),
                        cpCutVal = parseInt(resp['CurrentOutput' + codeVal]) - parseInt(resp['PlannedOutput' + codeVal]);
                    $cpCut.html(cpCutVal);
                    if (cpCutVal < 0) {
                        $cpCut.addClass('text-danger');
                    }
                    else {
                        $cpCut.removeClass('text-danger');
                    }
                }
            }
            if (resp['PlannedInput' + codeVal]) {
                var $plannedInput = $('#plannedInput' + codeVal);
                $plannedInput.html(resp['PlannedInput' + codeVal]);

                if (resp['CurrentInput' + codeVal]) {
                    var $cpCut = $('#cpCutInput' + codeVal),
                        cpCutVal = parseInt(resp['CurrentInput' + codeVal]) - parseInt(resp['PlannedInput' + codeVal]);
                    $cpCut.html(cpCutVal);
                    if (cpCutVal < 0) {
                        $cpCut.addClass('text-danger');
                    }
                    else {
                        $cpCut.removeClass('text-danger');
                    }
                }
            }
        }
    };

    var getShiftData = function () {
        var data = {
            rand: Date.now()
        };
        if (global.line_name) data['line_name'] = global.line_name;
        $.get('/NEP/PSS/api/product_info', data, function (resp) {
            if (resp) {
                var codeArr = [1, 2, 3];
                $.each(codeArr, function (idx, code) {
                    fillLineInfo(resp, code);
                });
            }
        }, 'json');
    };

    $(window).on('resize', function () {
        initLayout();
    });
})(jQuery, window);
